﻿//
// Copyright (C) Ashish Kaila 2011
//

using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using Flipbook.Commands;

namespace Flipbook.Behaviors
{
    /// <summary>
    /// Maps guestures to commands
    /// </summary>
    public class GestureToCommandBehavior : Behavior<FrameworkElement>
    {
        /// <summary>
        /// Command property
        /// </summary>
        public static DependencyProperty CommandProperty = DependencyProperty.Register("Command",
                                                                                       typeof(ICommand),
                                                                                       typeof(GestureToCommandBehavior));

        /// <summary>
        /// Command parameter property
        /// </summary>
        public static DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter",
                                                                                                typeof(object),
                                                                                                typeof(GestureToCommandBehavior));

        /// <summary>
        /// Initializes a new instance of the <see cref="GestureToCommandBehavior"/> class.
        /// </summary>
        public GestureToCommandBehavior()
        {
            GestureThresholdDistance = DefaultGestureThresholdDistance;
        }

        /// <summary>
        /// Gets or sets the command.
        /// </summary>
        /// <value>
        /// The command.
        /// </value>
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set
            {
                SetValue(CommandProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the command parameter.
        /// </summary>
        /// <value>
        /// The command parameter.
        /// </value>
        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }
            set
            {
                SetValue(CommandParameterProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the gesture threshold distance.
        /// </summary>
        /// <value>
        /// The gesture threshold distance.
        /// </value>
        public double GestureThresholdDistance
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the supported guestures
        /// </summary>
        /// <value>
        /// The guesture.
        /// </value>
        public SystemGesture SupportedGestures { get; set; }

        /// <summary>
        /// Called after the behavior is attached to an AssociatedObject.
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.AddHandler(UIElement.StylusSystemGestureEvent, new StylusSystemGestureEventHandler(OnGestureRecognized), false);
        }

        /// <summary>
        /// Called when gesture is recognized
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.StylusSystemGestureEventArgs"/> instance containing the event data.</param>
        private void OnGestureRecognized(object sender, StylusSystemGestureEventArgs e)
        {
            SystemGesture detectedGesture = e.SystemGesture;                                   

            if ((VerifyThresholdDelta(e)) && 
                ((detectedGesture & SupportedGestures) == detectedGesture))
            {
                GestureCommandParameter gestureCommandParameter = new GestureCommandParameter(e, CommandParameter);
                if ((Command != null) && (Command.CanExecute(gestureCommandParameter)))
                {
                    Command.Execute(gestureCommandParameter);
                }
            }
        }        

        /// <summary>
        /// Verifies the threshold delta.
        /// </summary>
        /// <param name="stylusArgs">The <see cref="System.Windows.Input.StylusSystemGestureEventArgs"/> instance containing the event data.</param>
        /// <returns></returns>
        private bool VerifyThresholdDelta(StylusSystemGestureEventArgs stylusArgs)
        {
            if ((SupportedGestures & SystemGesture.Flick) != SystemGesture.Flick)
            {
                return true;
            }

            Point currentPoint = stylusArgs.GetPosition(AssociatedObject);

            foreach (StylusPoint stylusPoint in stylusArgs.GetStylusPoints(AssociatedObject))
            {
                if ((stylusPoint.ToPoint() - currentPoint).Length >= GestureThresholdDistance)
                {
                    return true;
                }
            }

            return false;
        }

        // Private members
        private double DefaultGestureThresholdDistance = 1;
    }
}
